package my.OrderMessage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

public class DbGetData {
	
	 public String dbGetData(String httpAddress, List<NameValuePair> params )
	    {
	        HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httpPost = new HttpPost(httpAddress);
	        HttpResponse response;
	        String result = null;
	        
	        // POST 방법   
	        try {
//	        	List<NameValuePair> params = new ArrayList<NameValuePair>();
//        		params.add(new BasicNameValuePair("goodsName", arg[i]));	
            
	            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
	            response = httpclient.execute(httpPost);
	
	            // Get hold of the response entity
	            HttpEntity entity = response.getEntity();
	            // If the response does not enclose an entity, there is no need
	            // to worry about connection release

	            if (entity != null) {
	                // A Simple Response Read
	                InputStream instream = entity.getContent();
	                result = convertStreamToString(instream);
	
	                // Closing the input stream will trigger connection release
	                instream.close();
	            }
	        } catch (ClientProtocolException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	
	        return result;
	    }
	 
	  private static String convertStreamToString(InputStream is) {
	        /*
	        * To convert the InputStream to String we use the BufferedReader.readLine()
	        * method. We iterate until the BufferedReader return null which means
	        * there's no more data to read. Each line will appended to a StringBuilder
	        * and returned as String.
	        */
	        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
	        StringBuilder sb = new StringBuilder();

	        String line = null;
	        try {
	            while ((line = reader.readLine()) != null) {
	                sb.append(line + "\n");
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                is.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }

	        return sb.toString();
	    }

}
